/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.test.impl;

import org.sourceprojects.lycia.DefinitionBuilder;
import org.sourceprojects.lycia.ParserContext;
import org.sourceprojects.lycia.annotations.Tag;
import org.sourceprojects.lycia.annotations.TagParser;
import org.sourceprojects.lycia.annotations.XPath;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.w3c.dom.Element;

public class ComplexXPathTagParser {
	private static final String NAMESPACE = "http://www.example.com/schema/complex";

	@TagParser( { @Tag(xpath = @XPath(expression = "//*[@name='Add']"), namespace = NAMESPACE) })
	public void parseAdditionElement(final Element arg0,
			final ParserContext<StringBuilder> arg3,
			final DefinitionBuilder arg4) throws LyciaParsingException {
		for (int i = 0; i < arg0.getChildNodes().getLength(); i++) {
			if (!(arg0.getChildNodes().item(i) instanceof Element)) {
				continue;
			}

			final Element child = (Element) arg0.getChildNodes().item(i);

			final StringBuilder sb = arg3.getContextObject();

			sb.append("Addition: " + child.getTagName() + " - "
					+ child.getTextContent() + "\n");
		}
	}

	@TagParser( { @Tag(xpath = @XPath(expression = "//*[@name='Substract']"), namespace = NAMESPACE) })
	public void parseSubstractElement(final Element arg0,
			final ParserContext<StringBuilder> arg3,
			final DefinitionBuilder arg4) throws LyciaParsingException {
		for (int i = 0; i < arg0.getChildNodes().getLength(); i++) {
			if (!(arg0.getChildNodes().item(i) instanceof Element)) {
				continue;
			}

			final Element child = (Element) arg0.getChildNodes().item(i);

			final StringBuilder sb = arg3.getContextObject();

			sb.append("Substract: " + child.getTagName() + " - "
					+ child.getTextContent() + "\n");
		}
	}

	@TagParser( { @Tag(xpath = @XPath(expression = "//*[@name='Multiply']"), namespace = NAMESPACE) })
	public void parseMultiplyElement(final Element arg0,
			final ParserContext<StringBuilder> arg3,
			final DefinitionBuilder arg4) throws LyciaParsingException {
		for (int i = 0; i < arg0.getChildNodes().getLength(); i++) {
			if (!(arg0.getChildNodes().item(i) instanceof Element)) {
				continue;
			}

			final Element child = (Element) arg0.getChildNodes().item(i);

			final StringBuilder sb = arg3.getContextObject();

			sb.append("Multiply: " + child.getTagName() + " - "
					+ child.getTextContent() + "\n");
		}
	}

	@TagParser( { @Tag(xpath = @XPath(expression = "//*[@name='Divide']"), namespace = NAMESPACE) })
	public void parseDivideElement(final Element arg0,
			final ParserContext<StringBuilder> arg3,
			final DefinitionBuilder arg4) throws LyciaParsingException {
		for (int i = 0; i < arg0.getChildNodes().getLength(); i++) {
			if (!(arg0.getChildNodes().item(i) instanceof Element)) {
				continue;
			}

			final Element child = (Element) arg0.getChildNodes().item(i);

			final StringBuilder sb = arg3.getContextObject();

			sb.append("Divide: " + child.getTagName() + " - "
					+ child.getTextContent() + "\n");
		}
	}

}
